﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Cstudy.DAL
{
    public class resources
    {
        public bool AddRescource(MODEL.resources rescource, out int id)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@id",SqlDbType.Int),
                new SqlParameter("@title",rescource.Title),
                new SqlParameter("@content",rescource.Content),
                new SqlParameter("@authorid",rescource.AuthorID),
                new SqlParameter("@classid",rescource.ClassID)
            };
            parms[0].Direction = ParameterDirection.Output;
            return COM.SQLHelper.ExecuteNonQuery("sp_AddResource", parms, out id, "@id");
        }

        public bool DeleteRescource(int resid)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@resid",resid)
            };
            return COM.SQLHelper.ExecuteNonQuery("@sp_DeleteRescource", parms);
        }

        public bool UpdateRescoures(MODEL.resources rescource)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@resid",rescource.ResID),
                new SqlParameter("@title",rescource.Title),
                new SqlParameter("@content",rescource.Content),
                new SqlParameter("@classid",rescource.ClassID)
            };
            return COM.SQLHelper.ExecuteNonQuery("UpdateResource", parms);
        }

        public DataSet SearchRescources(string keyword)
        {
            keyword = "%" + keyword.Replace(" ", "%") + "%";
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@keyword",keyword)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SearchRescources",parms);
        }

        public DataSet SelectResources(int currentpage, int pagesize, out int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@count",SqlDbType.Int),
            };
            parms[0].Direction = ParameterDirection.Output;
            return COM.SQLTools.ExecuteQuery("sp_SelectResources", parms, currentpage, pagesize, out count, "@count");
        }

        public DataSet SelectResourcesByClassID(int classid, int currentpage, int pagesize, out int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@count",SqlDbType.Int),
                new SqlParameter("@classid",classid)
            };
            parms[0].Direction = ParameterDirection.Output;
            return COM.SQLTools.ExecuteQuery("sp_SelectResourcesByClassID", parms, currentpage, pagesize, out count, "@count");
        }

        public DataSet SelectResourceByResID(int resid)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@id",resid)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectResourceByResID", parms);
        }

        public bool AddResVisitTime(int resid)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@resid",resid)
            };
            return COM.SQLHelper.ExecuteNonQuery("sp_AddResVisitTime", parms);
        }

        public DataSet SelectHotRes(int count)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@count",count)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectHotRes", parms);
        }
    }
}
